package cn.webrx.bs.servlet;

import cn.webrx.bs.db.DruidUtil;
import cn.webrx.bs.entity.BookCates;
import cn.webrx.bs.service.AdDaoImpl;
import cn.webrx.bs.service.BookCatesImpl;
import cn.webrx.bs.service.BookDaoImpl;
import cn.webrx.bs.service.LinkDaoImpl;
import org.apache.commons.dbutils.QueryRunner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-04 17:39
 */
@WebServlet("/index")
public class Index extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QueryRunner run = new QueryRunner(DruidUtil.getDataSource());
        //友情链接
        req.setAttribute("links",new LinkDaoImpl().showAll());
        //轮播广告展示
        req.setAttribute("ads",new AdDaoImpl().show());


        req.setAttribute("books",new BookDaoImpl().show());

        req.setAttribute("cates",new BookCatesImpl().queryByParentId(0));

        Map<String,Object> map = new HashMap<String, Object>();
        for(BookCates c : new BookCatesImpl().queryByParentId(0)){
            map.put(c.getCname(),new BookDaoImpl().queryByParentId(c.getId()));
        }

        req.setAttribute("map",map);



        req.getRequestDispatcher("/home/index.ftl").forward(req,resp);
    }
}
